package com.qf.Interceptor;

import com.qf.bean.Employee;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import sun.security.util.Resources;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.ResourceBundle;

/**
 * 25/11/2019
 * Echo
 * qmxc
 * Alibaba
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
          //1.判断是否为匿名地址
        String uri = request.getRequestURI();
        ResourceBundle anno = Resources.getBundle("anno");
        if (anno.containsKey(uri)){
            return true;
        }
        //2.判断是否有session值
        HttpSession session = request.getSession();
        Employee uesr = (Employee)session.getAttribute("user");
        if (uesr!=null){
            return true;
        }else{
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.print("<script>alert('请先进行登录');location.href='/login.jsp';</script>");
            return false;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
